package com.gome.ocean.service.diamond.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gome.ocean.dao.diamond.ConfigInfoDAO;
import com.gome.ocean.dao.model.diamond.ConfigInfoBO;
import com.gome.ocean.service.diamond.ConfigInfoService;
import com.gome.ocean.service.diamond.NotifyService;
import com.taobao.diamond.md5.MD5;

@Service
@Transactional("diamondTransactionManager")
public class ConfigInfoServiceImpl implements ConfigInfoService {

    private final static MessageFormat TDATA_APP_TOPOLOGY = new MessageFormat("com.taobao.tddl.v1_{0}_dbgroups");
    private final static MessageFormat TGROUP_APP_TOPOLOGY = new MessageFormat("com.taobao.tddl.jdbc.group_V2.4.1_{0}");
    private final static MD5           INSTANCE       = MD5.getInstance();

    @Autowired
    private ConfigInfoDAO              configInfoDAO;
    @Autowired
    private NotifyService              notifyService;

    @Override
    public boolean saveConfigInfoBO(ConfigInfoBO configInfoBO) {
        String md5 = INSTANCE.getMD5String(configInfoBO.getContent());
        configInfoBO.setMd5(md5);
        configInfoBO.setGmtCreate(new Date());
        configInfoBO.setGmtModified(new Date());
        boolean result = configInfoDAO.saveConfigInfoBO(configInfoBO) > 0;
        if (result) {
            notifyService.notifyConfigInfoChange(configInfoBO.getDataId(), configInfoBO.getGroupId(), md5);
            return result;
        }
        return Boolean.FALSE;
    }

    @Override
    public boolean deleteConfigInfoBOById(Long id) {
        ConfigInfoBO configInfoBO = findConfigInfoBOById(id);
        String md5 = INSTANCE.getMD5String(configInfoBO.getContent());
        boolean result = configInfoDAO.deleteConfigInfoBOById(id) > 0;
        if (result) {
            notifyService.notifyConfigInfoChange(configInfoBO.getDataId(), configInfoBO.getGroupId(), md5);
            return result;
        }
        return Boolean.FALSE;
    }

    @Override
    public boolean updateConfigInfoBOById(ConfigInfoBO configInfoBO) {
        String md5 = INSTANCE.getMD5String(configInfoBO.getContent());
        configInfoBO.setMd5(md5);
        configInfoBO.setGmtModified(new Date());
        boolean result = configInfoDAO.updateConfigInfoBOById(configInfoBO) > 0;
        if (result) {
            notifyService.notifyConfigInfoChange(configInfoBO.getDataId(), configInfoBO.getGroupId(), md5);
            return result;
        }
        return Boolean.FALSE;
    }

    @Override
    public ConfigInfoBO findConfigInfoBOById(Long id) {
        return configInfoDAO.findConfigInfoBOById(id);
    }

    @Override
    public ConfigInfoBO findConfigInfoBOByDataId(String dataId) {
        return configInfoDAO.findConfigInfoBOByDataId(dataId);
    }

    @Override
    public ConfigInfoBO findConfigInfoBOByDataIdAndGroupId(String dataId, String groupId) {
        return configInfoDAO.findConfigInfoBOByDataIdAndGroupId(dataId, groupId);
    }

    @Override
    public ConfigInfoBO findConfigInfoBOByAppName(String appName) {
        String matrixKey = TDATA_APP_TOPOLOGY.format(new Object[] { appName });
        return findConfigInfoBOByDataId(matrixKey);
    }
    
    @Override
    public List<String> findShortAllApp() {
        String matrixKey = TDATA_APP_TOPOLOGY.format(new Object[] { "%" });
        List<ConfigInfoBO> configInfoBOs = configInfoDAO.findAllApp(matrixKey);
        if (configInfoBOs != null && !configInfoBOs.isEmpty()) {
            List<String> appNames = new ArrayList<String>();
            for (ConfigInfoBO configInfoBO : configInfoBOs) {
                appNames.add(StringUtils.substringBetween(configInfoBO.getDataId(), "com.taobao.tddl.v1_", "_dbgroups"));
            }
            return appNames;
        }
        return Collections.emptyList();
    }

    @Override
    public List<ConfigInfoBO> findAllApp() {
        String matrixKey = TDATA_APP_TOPOLOGY.format(new Object[] { "%" });
        List<ConfigInfoBO> configInfoBOs = configInfoDAO.findAllApp(matrixKey);
        if (configInfoBOs != null && !configInfoBOs.isEmpty()) {
            return configInfoBOs;
        }
        return Collections.emptyList();
    }

    @Override
    public List<ConfigInfoBO> findAllGroupApp() {
        String groupKey = TGROUP_APP_TOPOLOGY.format(new Object[] { "%" });
        List<ConfigInfoBO> configInfoBOs = configInfoDAO.findAllApp(groupKey);
        if (configInfoBOs != null && !configInfoBOs.isEmpty()) {
            return configInfoBOs;
        }
        return Collections.emptyList();
    }

	
	@Override
	public List<ConfigInfoBO> findAllApps(String dataId, int pageNum, int pageSize) {
		List<ConfigInfoBO> configInfoBOs = configInfoDAO.findAllApp(dataId,new RowBounds(pageNum, pageSize));
        if (configInfoBOs != null && !configInfoBOs.isEmpty()) {
            return configInfoBOs;
        }
        return Collections.emptyList();
	}

	@Override
	public List<ConfigInfoBO> findConfigInfoBOListByDataId(String dataId) {
		List<ConfigInfoBO> configInfoBOs = configInfoDAO.findAllApp(dataId);
        if (configInfoBOs != null && !configInfoBOs.isEmpty()) {
            return configInfoBOs;
        }
        return Collections.emptyList();
	}
}
